package chapt03.lstrips;
// LineStrips.java
// Demonstrates primitive GL_LINE_STRIP
// OpenGL SuperBible
// Richard S. Wright Jr.
// rwright@starstonesoftware.com

import javax.media.opengl.GL2;

public class LineStrips
{
	// Define a constant for the value of PI
	static float GL_PI = 3.1415f;
	
	// Rotation amounts
	static float xRot = 0.0f;
	static float yRot = 0.0f;
	
	// Setup the rendering state
	protected static void setup(GL2 gl2, int width, int height)
	{
		// Black background
		gl2.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
		
		// Set drawing color to green
		gl2.glColor3f(0.0f, 1.0f, 0.0f);
	}
	
	// Called to draw scene
	protected static void render(GL2 gl2, int w, int h)
	{
		float nRange = 100.0f;
		
		// Prevent a divide by zero
		if(h == 0)
			h = 1;
		
		// Set Viewport to window dimensions
		gl2.glViewport(0, 0, w, h);
		
		// Reset coordinate system
		gl2.glMatrixMode(GL2.GL_PROJECTION);
		gl2.glLoadIdentity();
		
		// Establish clipping volume (left, right, bottom, top, near, far)
		if(w <= h)
			gl2.glOrtho(-nRange, nRange, -nRange * h / w, nRange * h / w, -nRange, nRange);
		else
			gl2.glOrtho(-nRange * w / h, nRange * w / h, -nRange, nRange, -nRange, nRange);
		
		gl2.glMatrixMode(GL2.GL_MODELVIEW);
		gl2.glLoadIdentity();
		
		float x, y, z, angle; // Storage for coordinates and angles
		
		// Clear the window with the current clearing color
		gl2.glClear(GL2.GL_COLOR_BUFFER_BIT);
		
		// Save matrix state and do the rotation
		gl2.glPushMatrix();
		gl2.glRotatef(xRot, 1.0f, 0.0f, 0.0f);
		gl2.glRotatef(yRot, 0.0f, 1.0f, 0.0f);
		
		// Call only once for all remaining points
		gl2.glBegin(GL2.GL_LINE_STRIP);
		
		z = -50.0f;
		for(angle = 0.0f; angle <= (2.0f * GL_PI) * 3.0f; angle += 0.1f)
		{
			x = 50.0f * (float)Math.sin(angle);
			y = 50.0f * (float)Math.cos(angle);
			
			// Specify the point and move the Z value up a little
			gl2.glVertex3f(x, y, z);
			z += 0.5f;
		}
		
		// Done drawing points
		gl2.glEnd();
		
		// Restore transformations
		gl2.glPopMatrix();
		
		// Flush drawing commands
		gl2.glFlush();
		
	}
}